package com.bawei.property.view

import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.ListAdapter
import com.bawei.common.mvi.BaseMviFragment
import com.bawei.property.R
import com.bawei.property.adapter.ParkApplyListAdapter
import com.bawei.property.databinding.FragmentMineApplyBinding
import com.bawei.property.entity.ParkApplyValues
import com.bawei.property.intent.ParkApplyViewIntent
import com.bawei.property.state.ParkApplyUiState
import com.bawei.property.vm.ParkApplyViewModel
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class MineApplyFragment : BaseMviFragment<FragmentMineApplyBinding>() {

    private val mViewModel: ParkApplyViewModel by activityViewModels()
    private var list = mutableListOf<ParkApplyValues>()
    private lateinit var mListAdapter: ParkApplyListAdapter


    companion object {
        @JvmStatic
        fun newInstance() = MineApplyFragment()

    }

    override fun getLayoutId(): Int = R.layout.fragment_mine_apply

    override fun initView() {
        mDataBinding.rvParkApply.layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
        mListAdapter = ParkApplyListAdapter(R.layout.item_park_apply)
        mDataBinding.rvParkApply.adapter = mListAdapter

    }

    override suspend fun sendViewIntent() {
        mViewModel.channel.send(ParkApplyViewIntent.LoadParkApplyList)
    }


    override suspend fun collectUiState() {
       mViewModel.uiState.collect{
           when(it){
               is ParkApplyUiState.LoadParkApplyListSuccess -> {
                   list.addAll(it.data as MutableList<ParkApplyValues>)
                   mListAdapter.data.addAll(list)
                   mListAdapter.notifyDataSetChanged()
               }
               is ParkApplyUiState.LoadParkApplyListFail -> showToastMsg(it.msg)
               else -> {}
           }
       }
    }


}